<template>
  <div class="login-container">
    <van-nav-bar class="page-nav-bar" title="登录" />
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入用户名"
        type="number"
        maxlength="11"
        :rules="[
          {
            required: true,
            message: '手机号不可以为空',
          },
          {
            pattern: /^1[3|5|7|8|6]\d{9}$/,
            message: '手机号格式不对',
          },
        ]"
        ><i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        name="code"
        type="number"
        maxlength="11"
        v-model="user.code"
        left-icon="music-o"
        :rules="[
          {
            required: true,
            message: '短信验证码不可以为空',
          },
          {
            pattern: /^\d{6}$/,
            message: '短信验证码格式不对',
          },
        ]"
        placeholder="请输入验证码"
        ><i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down
            :time="5000"
            v-if="isCountDownShow"
            format="ss s"
            @finish="isCountDownShow = false"
          />
          <van-button
            v-else
            class="send-sms-btn"
            native-type="button"
            round
            size="small"
            type="default"
            @click="onSendSms"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit" class="login-btn"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user'
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data() {
    return {
      user: {
        mobile: '13911111111',
        code: '246810',
      },
      isCountDownShow: false,
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onSubmit() {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0,
      })
      try {
        const { data } = await login(this.user)
        console.log('res11: ', data.data)
        this.$toast.success('登录成功')
        this.$router.push('/my')
        this.$store.commit('setUser', data.data)
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机或验证码错误')
        } else {
          this.$toast.fail('登录失败')
        }
      }
    },
    async onSendSms() {
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (error) {
        return console.log('验证失败', err)
      }

      this.isCountDownShow = true
      try {
        const { data: res } = await sendSms(this.user.mobile)
        this.$toast.success('验证码发送成功')
        console.log('res22:', res)
      } catch (err) {
        this.isCountDownShow = false
        if (err.response.status === 429) {
          this.$toast('发送太频繁了，请稍后重试')
        }else{
          this.$toast('发送失败，请稍后重试')
        }
      }
    },
  },
}
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn {
    background-color: #6db4fb;
    border: none;
  }
}
</style>
